import { Injectable, HttpException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { JwtService } from '@nestjs/jwt';
import { adminAccount } from '../../entities/adminAccount.entity';
import { QueryUserDto } from './dto/login.dto';

@Injectable()
export class AboutService {
  constructor(
    @InjectRepository(adminAccount)
    private readonly aboutRepository: Repository<adminAccount>,
    private jwtService: JwtService,
  ) {}

  async login(data: QueryUserDto) {
    const { account, password } = data;
    const res = await this.aboutRepository.findOne({
      where: { account, password },
    });
    if (!res) {
      throw new HttpException({ message: '账号或密码错误' }, 500);
    }
    return {
      ...res,
      token: `Bearer ${this.jwtService.sign(JSON.parse(JSON.stringify(res)))}`,
    };
  }
  async getuserInfo(req: any) {
    return req.user;
  }
}
